import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-07-08
 * Time: 10:01
 */
public class MyStack {
    public int[] elem;
    public int size;

    public MyStack() {
        this.elem = new int[5];
    }
    //压栈
    public int push(int x)  {
        //判断栈是否满
        if (isFull()) {
            //二倍扩容
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[size++] = x;
        return 0;
    }
    private boolean isFull() {
        if (size == elem.length) {
            return true;
        }
        return false;
    }
    //出栈
    public int pop() {
        int a = elem[size-1];
        size--;
        return a;
    }
    //获取栈顶元素
    public int peek() {
        if (empty()) {
            throw new RuntimeException("栈为空,无法获取栈顶元素!");
        }
        int a = elem[size-1];
        return a;
    }
    //栈中有效元素的长度
    public int size() {
        return size;
    }
    //判断栈是否为空
    public boolean empty() {
        if(size == 0) {
            return true;
        }
        return false;
    }
}
